﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using BLL.Common;
using Enum;
using Admin;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class User_UserList : System.Web.UI.Page
{
    public string City = "城市";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // 绑定数据
            BindToDataGrid();
        }
    }

    private void BindToDataGrid()
    {
        // 获取所有用户
        List<UserProfileInfo> userProfiles = BLL.BUserProfile.getInstance().GetALLUserProfile();

        if (userProfiles != null)
        {
            this.UserDataGrid.DataSource = userProfiles;
            this.UserDataGrid.DataBind();
        }
    }

    protected void UserDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        this.UserDataGrid.CurrentPageIndex = e.NewPageIndex;
        BindToDataGrid();
    }
    protected void UserDataGrid_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        // 获取界面上修改值
        long passPortId = Convert.ToInt64(e.Item.Cells[3].Text);
        UserProfileInfo userProfiles = BLL.BUserProfile.getInstance().GetUserProfileInfo(passPortId);
        userProfiles.NickName = ((TextBox)(e.Item.Cells[5].Controls[0])).Text;
        userProfiles.Email = ((TextBox)(e.Item.Cells[6].Controls[0])).Text;
        userProfiles.Mobile = ((TextBox)(e.Item.Cells[7].Controls[0])).Text;
        //userProfiles.Education = ((TextBox)(e.Item.Cells[9].Controls[0])).Text;
        userProfiles.Adress = ((TextBox)(e.Item.Cells[14].Controls[0])).Text;
        //userProfiles.PersonalInCome = ((TextBox)(e.Item.Cells[13].Controls[0])).Text;
        //userProfiles.FamilyInCome = ((TextBox)(e.Item.Cells[14].Controls[0])).Text;
        userProfiles.LastUpdate = DateTime.Now;

        DropDownList ddl = (DropDownList)e.Item.FindControl("DllUserType");
        string value = ddl.SelectedValue;
        userProfiles.UserType = Convert.ToInt16(value);
        DropDownList personalInComeDdl = (DropDownList)e.Item.FindControl("DllPersonalInCome");
        string personalInComeValue = personalInComeDdl.SelectedValue;
        userProfiles.PersonalInCome = Convert.ToInt16(personalInComeValue);
        DropDownList familyIncomeDdl = (DropDownList)e.Item.FindControl("DllFamilyInCome");
        string familyIncomeValue = familyIncomeDdl.SelectedValue;
        userProfiles.FamilyInCome = Convert.ToInt16(familyIncomeValue);
        DropDownList educationDdl = (DropDownList)e.Item.FindControl("DllEducation");
        string educationValue = educationDdl.SelectedValue;
        userProfiles.Education = Convert.ToInt16(educationValue);

        BLL.BUserProfile.getInstance().Update(userProfiles);

        // 更新数值
        this.UserDataGrid.EditItemIndex = -1;
        this.BindToDataGrid();

    }
    protected void UserDataGrid_CancelCommand(object source, DataGridCommandEventArgs e)
    {
        this.UserDataGrid.EditItemIndex = -1;
        this.BindToDataGrid();
    }
    protected void UserDataGrid_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
        long passPortId = Convert.ToInt64(e.Item.Cells[3].Text);
        UserPassportInfo userPassportInfo = BLL.BUserPassport.getInstance().GetUserPassportInfo(passPortId);
        // 0表示删除，2表示拉黑（未完成）
        userPassportInfo.Status = 0;
        BLL.BUserPassport.getInstance().Update(userPassportInfo);
        BindToDataGrid();
    }
    protected void UserDataGrid_EditCommand(object source, DataGridCommandEventArgs e)
    {
        this.UserDataGrid.EditItemIndex = e.Item.ItemIndex;
        BindToDataGrid();
    }
    protected void UserDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.EditItem)
        {
            try
            {
                UserProfileInfo userInfo = (UserProfileInfo)(e.Item.DataItem);
                DropDownList userTypeDp = (DropDownList)e.Item.FindControl("DllUserType");
                userTypeDp.SelectedValue = Convert.ToString(userInfo.UserType);
                DropDownList personInComedp = (DropDownList)e.Item.FindControl("DllPersonalInCome");
                personInComedp.SelectedValue = Convert.ToString(userInfo.PersonalInCome);
                DropDownList familyInComeDp = (DropDownList)e.Item.FindControl("DllFamilyInCome");
                familyInComeDp.SelectedValue = Convert.ToString(userInfo.FamilyInCome);
                DropDownList educationDp = (DropDownList)e.Item.FindControl("DllEducation");
                educationDp.SelectedValue = Convert.ToString(userInfo.Education);

                // 初始化城市列表
                //DropDownList provinceDp = (DropDownList)e.Item.FindControl("DllProvinceId");
                //BindProvince(provinceDp);
                //DropDownList CityIdDp = (DropDownList)e.Item.FindControl("DllCityId");
                //BindCity(-1, CityIdDp);
            }
            catch (Exception ex)
            {
            }
        }
    }

    void BindProvince(DropDownList provinceDp)
    {
        string CurrentPath = this.Server.MapPath(".");
        if (System.IO.File.Exists(CurrentPath + "//Province.xml"))
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(CurrentPath + "//Province.xml");

            XmlNodeList nodes = doc.DocumentElement.ChildNodes;
            XmlNode nodeCity = doc.DocumentElement.SelectSingleNode(@"Province/City[@Name='" + this.City + "']");

            foreach (XmlNode node in nodes)
            {
                ListItem pItem = new ListItem();
                pItem.Text = node.Attributes["Name"].Value;
                pItem.Value = node.Attributes["Code"].Value;
                provinceDp.Items.Add(pItem);
                int n = provinceDp.Items.Count - 1;

                if (nodeCity != null && node == nodeCity.ParentNode)
                {
                    provinceDp.SelectedIndex = n;
                }
            }
            //bindCity(-1);
        }
        else
        {
        }
    }

    void BindCity(int selectIndex, DropDownList CityIdDp)
    {
        string CurrentPath = this.Server.MapPath(".");
        if (System.IO.File.Exists(CurrentPath + "//Province.xml"))
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(CurrentPath + "//Province.xml");

            XmlNodeList nodes = doc.DocumentElement.ChildNodes[selectIndex].ChildNodes;

            foreach (XmlNode node in nodes)
            {
                ListItem cItem = new ListItem();
                cItem.Text = node.Attributes["Name"].Value;
                cItem.Value = node.Attributes["Code"].Value;
                //this.DDL_city.Items.Add(node.Attributes["Name"].Value);
                CityIdDp.Items.Add(cItem);
                int n = CityIdDp.Items.Count - 1;
                if (node.Attributes["Name"].Value == this.City)
                {
                    CityIdDp.SelectedIndex = n;
                }
            }

            if (CityIdDp.SelectedIndex == -1)
            {
                CityIdDp.SelectedIndex = 0;
            }
        }
        else
        {
        }
    }

    //public string GetProvinceValue(string str_Value)
    //{
    //    string returnValue = "";
    //    try
    //    {
    //        string CurrentPath = this.Server.MapPath(".");
    //        if (System.IO.File.Exists(CurrentPath + "//Province.xml"))
    //        {
    //            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    //            doc.Load(CurrentPath + "//Province.xml");
    //            XmlNode nodProvince = doc.DocumentElement.SelectSingleNode(@"Province[@Code='" + str_Value + "']");

    //            if (nodProvince != null)
    //            {
    //                returnValue = nodProvince.Attributes["Name"].Value;
    //            }
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //    }
    //    return returnValue;
    //}

    //public string GetCityValue(string str_Value)
    //{
    //    string returnValue = "";
    //    try
    //    {
    //        string CurrentPath = this.Server.MapPath(".");
    //        if (System.IO.File.Exists(CurrentPath + "//Province.xml"))
    //        {
    //            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    //            doc.Load(CurrentPath + "//Province.xml");

    //            XmlNode nodCity = doc.DocumentElement.SelectSingleNode(@"Province/City[@Code='" + str_Value + "']");

    //            if (nodCity != null)
    //            {
    //                returnValue = nodCity.Attributes["Name"].Value;
    //            }
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //    }
    //    return returnValue;
    //}
}